<!-- 价格涨到 -->
<template>
  <div class="acenter all">
    <div class="acenter">
      <VPriceInputItem ref="input" :dPrice="dPrice" :min="minPrice" :max="maxPrice" style="width:690px"
        @onPriceChange="onPriceChange" />
      <WeexSlide class="slide" v-if="slideParams" :params="slideParams" @slideChange="slideChange"></WeexSlide>
    </div>
  </div>
</template>

<script>
  import VPriceInputItem from "@/aide/view/VPriceInputItem.vue";
  import jmodal from "@/_js/modal";
  import jconfig from "@/_js/config";
  import math from "@/_js/math";
  import I18N from "@/_i18n/i18n";
  import jdebug from "@/_js/debug";
  var T = I18N.t;
  export default {
    components: {
      VPriceInputItem,
    },

    props: {
      //条件
      condition: {
        type: Object,
        default: function () {
          return {};
        },
      },
      //当前价格
      dPrice: {
        type: String,
        default: "",
      },
    },

    data() {
      return {
        Config: jconfig,
        T: T,
        slideParams: undefined, //滑条参数
        maxPrice: 0,
        minPrice: 0,
        dLength: 0,
        price: 0,
      };
    },

    mounted() {
      this.maxPrice = Number(this.dPrice) * 2;
      this.minPrice = Number(this.dPrice);
      this.dLength = this.getDecimalPlaces(this.dPrice);
      if (this.condition.value && this.condition.value != "") {
        this.price = Number(this.condition.value);
        let nPrice = this.price.toFixed(this.dLength);
        this.$refs.input.price = nPrice;
      } else {
        this.price = Number(this.dPrice);
      }

      this.upSlideBar();
      this.updatePriceValue(false);
    },

    computed: {},

    methods: {
      onPriceChange(e) {
        this.price = Number(e);
        this.updatePriceValue(false);
        this.upSlideBar();
      },
      //更新滑条
      upSlideBar() {
        let starStr = this.dPrice;
        let endStr = this.dPrice;
        let ratio = (this.price + "").length
          ? math.chu(
              this.price - Number(this.dPrice) + "",
              endStr,
              this.dLength,
              2
            )
          : "0";

        ratio = math.getNumber(ratio) > 1 ? "1" : ratio;
        ratio = math.getNumber(endStr) == 0 ? 0 : ratio;
        if (ratio < 0) {
          ratio = 0;
        }
        this.slideParams = {
          start: starStr,
          end: this.maxPrice + "",
          ratio: ratio,
        };
      },
      slideChange(e) {
        this.price = Number(this.dPrice) + Number(this.dPrice) * e.ratio;
        this.updatePriceValue(true);
      },
      updatePriceValue(isUpdateInput) {
        let nPrice = this.price.toFixed(this.dLength);
        if (isUpdateInput) {
          this.$refs.input.price = nPrice;
        }
        this.$emit("onPriceChange", nPrice);
      },
      getDecimalPlaces(value) {
        if (!value.includes(".")) return 0; // 如果没有小数点，返回 0
        return value.split(".")[1].length; // 返回小数点后的位数
      },
      blur() {
        this.$refs.input && this.$refs.input.blur();
      },
    },
  };
</script>

<style src="@/_css/style.css" ></style>
<style scoped>
  .slide {
    width: 690px;
    height: 160px;
  }
  .channel {
    width: 690px;
  }
</style>
